package pages;

import core.TestBase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import ui_test.TestData;
import utils.Log4Test;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: u
 * Date: 7/20/14
 * Time: 11:10 PM
 * To change this template use File | Settings | File Templates.
 */
public class EditPage extends GeneralPage {
    protected By editPageTitle = By.xpath(".//h1[@class='personal-info-title']");
    protected By deliveryCityName = By.xpath("//input[@id='suggest_locality0']");
    protected By deliveryStreetName = By.xpath("//input[@name='addresses[0][street]']");
    protected By deliveryHouseNumber = By.xpath("//input[@name='addresses[0][house]']");
    protected By deliveryFlatNumber = By.xpath("//input[@name='addresses[0][flat]']");
    protected By submitButton = By.xpath("//div[@class='submit-block']//button[@type='submit']");
    protected By removeButton = By.xpath("//div/a[@class='sprite remove']");

    public void editPageTitleVerify() {
        Log4Test.info("Verifying edit page title ");
        webDriver.findElement(editPageTitle);
    }

    public void pastAaddressesFindAndRemove() {
        Log4Test.info("removing earlier addresses if they were present");
        List<WebElement> removeButtons = webDriver.findElements(removeButton);
        if (removeButtons.size() > 0) {
            for (WebElement cross : removeButtons) {
                cross.click();
            }
        }

    }

    public void deliveryAddressChange() {
        Log4Test.info("Changing user address settings");


        webDriver.findElement(deliveryCityName).clear();
        webDriver.findElement(deliveryCityName).sendKeys(TestData.deliveryCity);
        webDriver.findElement(deliveryStreetName).sendKeys(TestData.deliveryStreet);
        webDriver.findElement(deliveryHouseNumber).sendKeys(TestData.deliveryHouse);
        webDriver.findElement(deliveryFlatNumber).sendKeys(TestData.deliveryFlat);

    }


    public SavedDataPage submitClick() {
        Log4Test.info("transferring to page with saved data");
        webDriver.findElement(submitButton).click();
        return new SavedDataPage();
    }

}
